package org.to_zero.springboot.jwt.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @Author moonholder
 * @Description //跨域资源访问
 * @Date 16:26 2020/12/10
 */
@Configuration
@Order(1)
public class CorsFilterConfig {
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }

    private CorsConfiguration buildConfig() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.addAllowedHeader("*"); //允许任何header
        configuration.addAllowedMethod("*"); //允许任和请求方法
        configuration.addAllowedOrigin("*"); //允许任何域名的请求
        configuration.setAllowCredentials(true);
        return configuration;
    }
}
